package com.wanhe.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.trello.rxlifecycle2.LifecycleTransformer;
import com.trello.rxlifecycle2.components.support.RxFragment;
import com.wanhe.base.injector.component.BaseApplicationComponent;

/**
 * Created by XieZaiWei on 2017/6/14.
 * fragment的基类
 */

public abstract class BaseFragment extends RxFragment implements IBaseView{

	protected Context mContext;
	protected View mRootView;

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = getActivity();
	}

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		if (mRootView == null) {
			mRootView = inflater.inflate(attachLayoutRes(), null);
			initViews();
		}
		ViewGroup parent = (ViewGroup) mRootView.getParent();
		if (parent != null) {
			parent.removeView(mRootView);
		}
		return mRootView;
	}

	protected BaseApplicationComponent getAppComponent(){
		return BaseApplication.getApplicationComponent();
	}

	/**
	 * 绑定布局文件
	 * @return  布局文件ID
	 */
	protected abstract int attachLayoutRes();

	/**
	 * 初始化视图控件
	 */
	protected abstract void initViews();

	@Override
	public void showLoading() {
	}

	@Override
	public void hideLoading() {
	}

	@Override
	public void showNetError() {
	}


	@Override
	public <T> LifecycleTransformer<T> bindToLife() {
		return this.<T>bindToLifecycle();
	}

}
